home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Click0.java < prev    next >
Text File  |  1998-09-15  |  1KB  |  54 lines

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5.  
  6. /** 
  7.  * Here's a simple example of an anonymous nested class that implements
  8.  * the MouseMotionListener interface.  It does so by extending 
  9.  * MouseMotionAdapter, a utility class that provides no-op implementations
  10.  * for all of the methods in MouseMotionListener.  In this case we're
  11.  * just handling mouseMoved() events by moving the "puck" along the
  12.  * bottom edge of the applet.
  13.  * 
  14.  * Note that the listener implementation can refer to fields defined
  15.  * in enclosing scopes, e.g. the Box field called puck, directly.  
  16.  * 
  17.  * This applet runs correctly in HotJava, it requires JDK 1.1.
  18.  */
  19.  
  20. public class Click0 extends Applet
  21. {
  22.   Color puckColor = new Color(200, 0, 10);
  23.   Box puck = new Box(puckColor);
  24.  
  25.   public Click0()
  26.   {
  27.     MouseMotionListener movePuck = new MouseMotionAdapter() {
  28.       public void mouseMoved(MouseEvent e)
  29.       {
  30.     int x = e.getX();
  31.     int y = getSize().height - puck.getSize().height;
  32.     puck.setLocation(x, y);
  33.       }
  34.     };
  35.  
  36.     add(puck);
  37.     addMouseMotionListener(movePuck);
  38.   }
  39.  
  40.   public static void main(String[] args)
  41.   {
  42.     WindowListener l = new WindowAdapter()
  43.       {
  44.     public void windowClosing(WindowEvent e) {System.exit(0);}
  45.       };
  46.  
  47.     Frame f = new Frame("Click");
  48.     f.addWindowListener(l); 
  49.     f.add(new Click0());
  50.     f.setSize(400, 400);
  51.     f.show();
  52.   }
  53. }
  54.